package com.jandan.ui.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.gwtwidgets.server.spring.ServletUtils;

import com.jandan.logic.JWordzFacade;
import com.jandan.ui.client.service.StrangeWordService;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.LearnedWord;
import com.jandan.ui.model.StrangeWordGroupInfo;
import com.jandan.ui.model.Word;
import com.jandan.web.front.UserSession;

public class StrangeWordServiceImpl implements StrangeWordService {
	private JWordzFacade jwordz;

	public void setJwordz(JWordzFacade jwordz) {
		this.jwordz = jwordz;
	}

	public List<Word> getAllStrangeWordList() {
		// TODO Auto-generated method stub
		HttpSession session = ServletUtils.getRequest().getSession(true);
		UserSession userSession = (UserSession) session
				.getAttribute("userSession");

		if (userSession != null) {
			long userID = userSession.getAccount().getUserID();
			return jwordz.getAllStrangeWordList(userID);
		}

		return new ArrayList<Word>();
	}

	public List<LearnedWord> getAllLearnedStrangeWordList() {
		// TODO Auto-generated method stub
		HttpSession session = ServletUtils.getRequest().getSession(true);
		UserSession userSession = (UserSession) session
				.getAttribute("userSession");

		if (userSession != null) {
			long userID = userSession.getAccount().getUserID();
			return jwordz.getAllLearnedStrangeWordList(userID);
		}

		return new ArrayList<LearnedWord>();
	}

	public List<Word> getNewStrangeWordList(int groupNumber) {
		// TODO Auto-generated method stub
		HttpSession session = ServletUtils.getRequest().getSession(true);
		UserSession userSession = (UserSession) session
				.getAttribute("userSession");

		if (userSession != null) {
			long userID = userSession.getAccount().getUserID();
			return jwordz.getNewStrangeWordList(userID, groupNumber);
		}

		return new ArrayList<Word>();
	}

	public List<LearnedWord> getLearnedStrangeWordList(int groupNumber) {
		// TODO Auto-generated method stub
		HttpSession session = ServletUtils.getRequest().getSession(true);
		UserSession userSession = (UserSession) session
				.getAttribute("userSession");

		if (userSession != null) {
			long userID = userSession.getAccount().getUserID();
			return jwordz.getLearnedStrangeWordList(userID, groupNumber);
		}
		return new ArrayList<LearnedWord>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.jandan.ui.client.service.StrangeWordService#
	 * getNewStrangeWordAndLearnedStrangeWordList(int)
	 */
	public Map<String, List> getNewStrangeWordAndLearnedStrangeWordList(
			int groupNumber) {
		// TODO Auto-generated method stub

		List<Word> newStrangeWordList = this.getNewStrangeWordList(groupNumber);
		List<LearnedWord> learnedStrangeWordList = this
				.getLearnedStrangeWordList(groupNumber);

		Map<String, List> map = new HashMap<String, List>();
		map.put("newStrangeWordList", newStrangeWordList);
		map.put("learnedStrangeWordList", learnedStrangeWordList);

		return map;
	}

	public List<StrangeWordGroupInfo> getStrangeWordGroupInfoList() {
		// TODO Auto-generated method stub
		HttpSession session = ServletUtils.getRequest().getSession(true);
		UserSession userSession = (UserSession) session
				.getAttribute("userSession");

		if (userSession != null) {
			long userID = userSession.getAccount().getUserID();
			List<StrangeWordGroupInfo> strangeWordGroupInfoList = new ArrayList<StrangeWordGroupInfo>();

			// 先获取count,再获取每个分组的startWord和endWord
			int totalStrangeWordCount = jwordz
					.getStrangeWordCountByUserID(userID);
			int n = (int) (totalStrangeWordCount / ClientUtil.STRANGE_WORD_GROUP_SIZE);
			StrangeWordGroupInfo strangeWordGroupInfo = null;
			Word startWord = null;
			Word endWord = null;
			for (int i = 0; i <= n; i++) {
				int startNumber = i * ClientUtil.STRANGE_WORD_GROUP_SIZE;
				int endNumber = (i + 1) * ClientUtil.STRANGE_WORD_GROUP_SIZE
						- 1;
				if (i == n) {
					endNumber = totalStrangeWordCount - 1;
				}

				strangeWordGroupInfo = new StrangeWordGroupInfo();
				startWord = jwordz.getStrangeWordOfOnePositionByUserID(
						userID, startNumber);
				endWord = jwordz.getStrangeWordOfOnePositionByUserID(
						userID, endNumber);
				if (startWord != null && endWord != null) {
					strangeWordGroupInfo.setStartWord(startWord.getWordEn());
					strangeWordGroupInfo.setEndWord(endWord.getWordEn());
					strangeWordGroupInfo.setTotalWordCount(endNumber
							- startNumber + 1);
					strangeWordGroupInfo.setGroupNumber(i);

					strangeWordGroupInfoList.add(strangeWordGroupInfo);
				}
			}

			return strangeWordGroupInfoList;
		}
		return new ArrayList<StrangeWordGroupInfo>();
	}
}
